{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fd8cba0d-ecf6-4f82-af83-4d99ec062e60",
   "metadata": {},
   "source": [
    "# 字典数据类型\n",
    "键值对的集合，键是**唯一**的区分**大小写**。\n",
    "\n",
    "**字典值可以是任何的 python 对象，既可以是标准的对象，也可以是用户定义的，但键不行。**"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2147940d-9455-4c84-aa95-b96661a48ad6",
   "metadata": {},
   "source": [
    "![img](https://www.runoob.com/wp-content/uploads/2016/04/py-dict-3.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19e287da-552e-47a9-8752-c2502e3ff99a",
   "metadata": {},
   "source": [
    "## 创建字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "fea24731-b747-4032-be8c-12049ad9f74f",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "dict1 = {}\n",
    "dict2 = dict()\n",
    "dict3= {'name': '小明', 'likes': 123, 'url': 'www.baidu.com'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "c97e7197-dead-44b1-aee6-960f02475994",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n",
      "{}\n",
      "{'name': '小明', 'likes': 123, 'url': 'www.baidu.com'}\n"
     ]
    }
   ],
   "source": [
    "print(dict1)\n",
    "print(dict2)\n",
    "print(dict3)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2ffa95d9-21c9-46db-8370-5c0357b45134",
   "metadata": {},
   "source": [
    "## 查看字典内元素\n",
    "因为是键值对，所以可以使用键去找值，且键是唯一的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "2cd05b8b-7498-4063-bb7a-9e9fb5f5ab79",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'小明'"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict3[\"name\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "be0453af-8cdd-4b06-abf5-9d54255fc772",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'小明'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict3.get(\"name\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a68df2ff-095c-422d-b140-8e047178ba04",
   "metadata": {},
   "source": [
    "可以通过键名来找到值  如何知道有什么键呢"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "a2274ab4-fc19-4414-9796-d70f7c28b293",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['name', 'likes', 'url'])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看键名\n",
    "dict3.keys()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "de4ba477-08b7-4296-9605-8881e7e2705d",
   "metadata": {},
   "source": [
    "## 添加\\修改字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "ef67f729-9a1f-4edd-8202-9e401c41c263",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': '张三', 'Age': 18, 'School': '小学'}\n"
     ]
    }
   ],
   "source": [
    "dict1[\"name\"] = \"张三\"\n",
    "dict1['Age'] = 18               # 更新 Age\n",
    "dict1['School'] = \"小学\"  # 添加信息 \n",
    "print(dict1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "67f097df-7830-49f4-9723-05c1fa513eba",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "dict1['Age'] = 8               # 更新 Age\n",
    "dict1['School'] = \"大学\"  # 添加信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "c07344e7-64d9-4a99-99ab-ee74a650b99a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': '张三', 'Age': 8, 'School': '大学'}\n"
     ]
    }
   ],
   "source": [
    "print(dict1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd752bdd-58c0-45f2-922b-80b1f74cb4c2",
   "metadata": {},
   "source": [
    "## 删除字典元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "8df063a0-dec1-4b89-a6af-587847faf66a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Age': 8, 'School': '大学'}\n"
     ]
    }
   ],
   "source": [
    "del dict1['name'] # 删除键 'name'\n",
    "print(dict1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "4f42b734-bfe3-47e7-a320-e5f3358cc006",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n"
     ]
    }
   ],
   "source": [
    "dict1.clear()     # 清空字典\n",
    "print(dict1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "aa01d00f-12c3-4085-b9a4-bba25f6d034d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'dict1' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[60], line 2\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;28;01mdel\u001b[39;00m dict1         \u001b[38;5;66;03m# 删除字典\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m \u001b[38;5;28mprint\u001b[39m(dict1)\n",
      "\u001b[1;31mNameError\u001b[0m: name 'dict1' is not defined"
     ]
    }
   ],
   "source": [
    "del dict1         # 删除字典\n",
    "print(dict1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c32d8527-b9be-4615-aced-973c66767547",
   "metadata": {},
   "source": [
    "## 内置函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a0c5245f-b6d2-45fc-8a53-f2275e129d00",
   "metadata": {},
   "source": [
    "| 序号 | 函数及描述                                                   | 实例                                                         |\n",
    "| :--- | :----------------------------------------------------------- | :----------------------------------------------------------- |\n",
    "| 1    | len(dict) 计算字典元素个数，即键的总数。                     | `>>> tinydict = {'Name': 'Runoob', 'Age': 7, 'Class': 'First'} >>> len(tinydict) 3` |\n",
    "| 2    | str(dict) 输出字典，可以打印的字符串表示。                   | `>>> tinydict = {'Name': 'Runoob', 'Age': 7, 'Class': 'First'} >>> str(tinydict) \"{'Name': 'Runoob', 'Class': 'First', 'Age': 7}\"` |\n",
    "| 3    | type(variable) 返回输入的变量类型，如果变量是字典就返回字典类型。 | `>>> tinydict = {'Name': 'Runoob', 'Age': 7, 'Class': 'First'} >>> type(tinydict) <class 'dict'>` |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "005244bc-25f4-4eec-b553-a569b97fadb4",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(dict3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "6ba19434-ecce-4bc0-a3f0-21f27fbe3cad",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"{'name': '小明', 'likes': 123, 'url': 'www.baidu.com'}\""
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str(dict3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "c0271b78-0c64-4351-ad75-128252b0658f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(dict3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "1420f225-a5d6-4c59-bd6f-083967555414",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['name', 'likes', 'url'])"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看键名\n",
    "dict3.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "2e213813-1e9b-4e37-a7e6-b0017e9ec56e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_values(['小明', 123, 'www.baidu.com'])"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict3.values()  # 查看全部值"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ce859397-097c-4796-8e4b-2c443197fcab",
   "metadata": {},
   "source": [
    "| 序号 | 函数及描述                                                   |\n",
    "| :--- | :----------------------------------------------------------- |\n",
    "| 1    | [dict.clear()](https://www.runoob.com/python3/python3-att-dictionary-clear.html) 删除字典内所有元素 |\n",
    "| 2    | [dict.copy()](https://www.runoob.com/python3/python3-att-dictionary-copy.html) 返回一个字典的浅复制 |\n",
    "| 3    | [dict.fromkeys()](https://www.runoob.com/python3/python3-att-dictionary-fromkeys.html) 创建一个新字典，以序列seq中元素做字典的键，val为字典所有键对应的初始值 |\n",
    "| 4    | [dict.get(key, default=None)](https://www.runoob.com/python3/python3-att-dictionary-get.html) 返回指定键的值，如果键不在字典中返回 default 设置的默认值 |\n",
    "| 5    | [key in dict](https://www.runoob.com/python3/python3-att-dictionary-in.html) 如果键在字典dict里返回true，否则返回false |\n",
    "| 6    | [dict.items()](https://www.runoob.com/python3/python3-att-dictionary-items.html) 以列表返回一个视图对象 |\n",
    "| 7    | [dict.keys()](https://www.runoob.com/python3/python3-att-dictionary-keys.html) 返回一个视图对象 |\n",
    "| 8    | [dict.setdefault(key, default=None)](https://www.runoob.com/python3/python3-att-dictionary-setdefault.html) 和get()类似, 但如果键不存在于字典中，将会添加键并将值设为default |\n",
    "| 9    | [dict.update(dict2)](https://www.runoob.com/python3/python3-att-dictionary-update.html) 把字典dict2的键/值对更新到dict里 |\n",
    "| 10   | [dict.values()](https://www.runoob.com/python3/python3-att-dictionary-values.html) 返回一个视图对象 |\n",
    "| 11   | [pop(key[,default])](https://www.runoob.com/python3/python3-att-dictionary-pop.html) 删除字典 key（键）所对应的值，返回被删除的值。 |\n",
    "| 12   | [popitem()](https://www.runoob.com/python3/python3-att-dictionary-popitem.html) 返回并删除字典中的最后一对键和值。 |"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
